<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\FunctionController;
use App\Models\AuthRelation;
use App\Models\AuthRole;
use App\Models\AuthRule;
use App\Utils\CodeUtil;
use App\Utils\ConstantUtil;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RoleController extends Controller
{
    /**
     * 显示资源列表
     */
    public function index(Request $request)
    {
        $select['title'] = $request->input("title");
        $list = AuthRole::orderBy("id","desc");
        if (!empty($select['title'])) {
            $list = $list->where("title", "LIKE", '%' . $select['title'] . '%');
        }
        $list = $list->paginate(ConstantUtil::PAGE_SHOW);
        return view('Api/role/index',[
            'list'   => $list,
            'select' => $select,
            "status" => ConstantUtil::getStatus()
            //'protect' => Config::get("dw_init.auth_role_protect")
        ]);
    }

    /**
     * 显示创建资源表单页.
     */
    function create()
    {
        return view('Api/role/create',[
            "status" => ConstantUtil::getStatus()
            //'protect' => Config::get("dw_init.auth_role_protect")
        ]);
    }
    /**
     * 保存新建的资源
     */
    function save(Request $request)
    {
        $data = $request->input();
        AuthRole::insert($data);
        return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "添加成功!"]);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
    }

    /**
     * 显示编辑资源表单页.
     */
    function edit(Request $request)
    {
        $list = AuthRole::find($request->input("id"));
        return view('Api/role/edit',[
            "status" => ConstantUtil::getStatus(),
            'list'  => $list,
        ]);
    }

    /**
     * 保存更新的资源
     */
    function update(Request $request)
    {
        $id = $request->input("id");
        $data = $request->input();
        unset($data["id"]);
        AuthRole::where("id",$id)->update($data);
        return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "保存成功!"]);
    }

    /**
     * 删除指定资源
     */
    public function delete(Request $request)
    {
        $id = $request->input("id");
        if (!is_array($id)) $id = [$id];
        AuthRole::whereIn("id",$id)->delete();
        return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "删除成功!"]);
    }

    /**
     * 获取角色-用户
     * @param $id
     */
    function admin($id){
        $list = AuthRelation::join("user","user.id","=","auth_relation.uid")
            ->where("auth_relation.group_id",$id)
            ->get()->toArray();

        /*$list = Db::name("AuthRelation")->alias('a')->field("b.username")
            ->join("__ADMIN__ b","a.uid = b.id")->where("group_id","EQ",$id)
            ->select();
        if(!empty($list)){
            $admin = array_column($list,'username');
            $this->success('success','',$admin);
        }else{
            $this->success('error');
        }*/

    }

    /**
     * 权限分配页面
     *
     * @return mixed
     */
    public function permission(Request $request)
    {
        $id = $request->input('id');
        $roleRes = AuthRole::find($id);
        if (is_null($roleRes)) abort(404, "数据不存在!");
        $ruleRes      = AuthRule::where("status",1)->orderBy("sort","desc")->get()->toArray();
        $ruleRes      = FunctionController::generate_tree($ruleRes);
        $currentRules = [];
        if (!empty($roleRes['rules'])) {
            $currentRules = $hasRules = explode(',', $roleRes['rules']);
        }
        return view('Api/role/permission',[
            'roleRes'      => $roleRes,
            'ruleRes'      => $ruleRes,
            'currentRules' => $currentRules,
        ]);
    }

    /**
     * 权限分配
     */
    public function setPermission(Request $request)
    {
        $id = $request->input('id');
        $ruleIds = $request->input("rule_id");
        if(!empty($ruleIds)){
            $data['rules'] = implode(',',$ruleIds);
        }else{
            $data['rules'] = '';
        }
        AuthRole::where("id",$id)->update($data);
        return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "保存成功!"]);
    }
}
